package com.ruoyi.mymod.controller.patrol;


import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.mymod.service.IPatrolService;
import com.ruoyi.mymod.service.IPecProgramService;
import com.ruoyi.mymod.utils.ProgExecContr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * 测试功能
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/patrol")
public class PatrolController extends BaseController
{

    @Autowired
    private IPatrolService patrolService;
    /**
     * 获取菜单列表
     */
    @GetMapping("/getSitesList")
    public AjaxResult getSitesList(@RequestParam(required = false) String phone) throws Exception {
        String json=patrolService.getSitesList(phone);
        return success(json);
    }

    @PostMapping("/uploadImage")
    public AjaxResult uploadImage(@RequestParam("file") MultipartFile file,
                                  @RequestParam(value = "fileName", required = false) String fileName) throws Exception {
        String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            return success(avatar);

    }

    @GetMapping("/doPatrol")
    public AjaxResult doPatrol(@RequestParam(required = false) String phone,@RequestParam(required = false) String route,@RequestParam(required = false) String site,@RequestParam(required = false) String pics,@RequestParam(required = false) String description) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowdate = format.format(new Date());
        if (pics==null || "".equals(pics) || ",".equals(pics)){
            pics="";
        }else {
            if (!pics.isEmpty()) {
                pics = pics.substring(0, pics.length() - 1);
            }
        }
        String json=patrolService.doPatrol(phone,nowdate,route,site,pics,description);
        return success(json);
    }


    @GetMapping("/getUsers")
    public AjaxResult getUsers() throws Exception {
        String json=patrolService.getUsers();
        return success(json);
    }


    @GetMapping("/getRecord")
    public AjaxResult getRecord(@RequestParam(required = false) String phone,@RequestParam(required = false) String date) throws Exception {
        String json=patrolService.getRecord(phone,date);
        return success(json);
    }

}
